package com.cxf.windex.base.util;

import java.text.ParseException;

/**
 * 经纬度距离
 * 经纬度方向
 * @author cxf
 *
 */
public class JWDUtil {
	private static double EARTH_RADIUS =  6371;

	private static double pai = 3.1415926536;
	public static String getStrDistance(String wd1, String jd1, String wd2, String jd2){
		String dist=""+getDistance(Double.parseDouble(wd1),Double.parseDouble(jd1),Double.parseDouble(wd2),Double.parseDouble(jd2));
		if(dist.equals("0")){
			dist="1";
		}
		return dist;
	}

	public static Integer getDistance(double wd1, double jd1, double wd2, double jd2)
	{
		double hd1 = 2 * pai *  wd1/ 360;
		double hd2 = 2 * pai *  wd2/ 360;
	    double jd21 = 2 * pai * (jd2 - jd1) / 360;

	    double pfg = (1 - (Math.cos(hd1) * Math.cos(hd2) * Math.cos(jd21) +Math.sin(hd1) * Math.sin(hd2)));
	    if( pfg >= 0){

	    	double s=(
	    			EARTH_RADIUS * Math.sqrt(2 * (1 - (Math.cos(hd1) * Math.cos(hd2) * Math.cos(jd21) + Math.sin(hd1) * Math.sin(hd2))))+0.5);

	    	return (int)s;
	    }
	    return null;
	}

	public static String getFx(double wd1, double jd1, double wd2, double jd2)
	{
		if(jd1 - jd2 == 0 ){
	        if( wd1 - wd2 > 0 ){
	            return "偏北";
	        }else if( wd1 - wd2 < 0 ){
	            return "偏南";
	        }else{
	            return  "未知";
	        }
		}else{
			 double dValTan = (wd1 - wd2) / (jd1 - jd2);
			 double angle = Math.atan(dValTan) * 180.0/ pai;
			 if( (jd1 - jd2) < 0 ){
				  angle = angle + 180;
			 }
			 if( angle < 0 ){
				 angle = angle + 360;
			 }
			 if( angle >= 11.25 && angle < 33.75 ){
				 return "东北偏东";
			 }else if( angle >= 33.75 && angle < 56.25){
				 return "东北";
			 }else if(angle >= 56.25 && angle < 78.75){
				 return  "东北偏北";
			 }else if(angle >= 78.75 && angle < 101.25){
				 return  "偏北";
			 }else if(angle >= 101.25 && angle < 123.75){
				 return  "西北偏北";
			 }else if(angle >= 123.75 && angle < 146.25){
				 return  "西北";
			 }else if(angle >= 146.25 && angle < 168.75){
				 return  "西北偏西";
			 }else if(angle >= 168.75 && angle < 191.25){
				 return  "西南偏西";
			 }else if(angle >= 213.75 && angle < 236.25){
				 return  "西南";
			 }else if(angle >= 236.25 && angle < 258.75){
				 return  "西南偏南";
			 }else if(angle >= 258.75 && angle < 281.25){
				 return  "偏南";

			 }else if(angle >= 281.25 && angle < 303.75 ){
				 return  "东南偏南";
			 }else if(angle >= 303.75 && angle < 326.25 ){
				 return  "东南";
			 }else if( angle >= 326.25 && angle < 348.75 ){
				 return  "东南偏东";
			 }else{
				 return  "偏东";
			 }
		}
	}

	public static void main(String[] args) throws ParseException {
		//jd2=121.56    121.57
		//		  wd2=29.82 29.87
		String lng1 = "121.56865", lat1 = "29.89843", lng2 = "121.54748", lat2 = "29.80926";
		String s = JWDUtil.getStrDistance(lat1, lng1, lat2, lng2);
		//String str=JWDUtil.getFx(lat1, lng1, lat2, lng2);
		System.out.println(s+"");

	}
}
